<template>
  <div style="height: 100%">
    <Info v-show="props.isAdmin" class="info" ref="infoRef" v-slot="slotProps">
      <Table
        class="table"
        :tableData="slotProps.tableData"
        :editInstructs="openDrawWithEdit"
        :triggerServer="triggerServer"
        :changeServerPosition="changeServerPosition"
        :schoolMap="slotProps.schoolMap"
        :refresh="slotProps.refresh"
      ></Table>
    </Info>
    <Edit
      ref="editRef"
      :show="isShowDraw"
      :title="drawTitle"
      :defaultInfo="defaultInfo"
      @visibleChange="(visble) => (isShowDraw = visble)"
      @submitSuccess="submitSuccess"
    ></Edit>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import {
  api_changeServerPosition,
  api_youxianjiaru,
} from "../../../api/manage";
import Edit from "./Edit.vue";
import Info from "./Info.vue";
import Table from "./Table.vue";

const props = defineProps({
  isAdmin: Boolean,
});

const infoRef = ref();
const editRef = ref();
const isShowDraw = ref(false);
const drawTitle = ref("");
const defaultInfo = ref(null);

defineExpose({
  refresh: () => infoRef.value.refresh(),
  init: () => editRef.value.init(),
});

const openDrawWithEdit = (index, row) => {
  defaultInfo.value = row;
  drawTitle.value = "编辑一网通办常见语音指令";
  isShowDraw.value = true;
};
const submitSuccess = () => {
  infoRef.value.refresh();
};

const triggerServer = async (data) => {
  let result = "";
  await api_youxianjiaru(data)
    .then((res) => {
      console.log("api_youxianjiaru res = ", res);
      result = res;
    })
    .catch((err) => {
      console.log("api_youxianjiaru err = ", err);
    });
  return result;
};

const changeServerPosition = async (data) => {
  let result = "";
  await api_changeServerPosition(data)
    .then((res) => {
      result = res;
    })
    .catch((err) => {});
  return result;
};
</script>

<style scoped lang="scss">
.table {
  padding: 0 30px;
}
</style>
